﻿using System;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace BookMS
{
    public partial class LoginForm : Form
    {
        public static Action showForm;

        private string LoginCAPTCHA;

        public LoginForm()
        {
            InitializeComponent();
        }

        private void ShowLoginForm()
        {
            this.Show();
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            showForm = ShowLoginForm;

            UpdateCAPTCHA();

            int result = (int)SqlHelper.ExecuteScalar("select 1");
            if (result == 1)
            {
                Label_ConnState.Text = "数据库连接成功";
            }
            else
            {
                this.Close();
            }
        }

        private void LoginForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            // 清空全部连接池
            SqlConnection.ClearAllPools();
        }

        private void UpdateCAPTCHA()
        {
            LoginCAPTCHA = Guid.NewGuid().ToString().Substring(0, 5).ToLower();
            LinkLabel_CAPTCHA.Text = LoginCAPTCHA;
        }

        private void LinkLabel_CAPTCHA_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            UpdateCAPTCHA();
        }

        private void Button_Login_Click(object sender, EventArgs e)
        {
            string user_name = TextBox_UserName.Text.Trim();
            string user_password = TextBox_UserPassword.Text.Trim();

            string CAPTCHA = TextBox_CAPTCHA.Text.Trim();

            if (string.IsNullOrEmpty(user_name) || string.IsNullOrEmpty(user_password))
            {
                MessageBox.Show("用户名或密码为空", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                UpdateCAPTCHA();
                return;
            }

            if (string.IsNullOrEmpty(CAPTCHA))
            {
                MessageBox.Show("请输入验证码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                UpdateCAPTCHA();
                return;
            }

            if (!CAPTCHA.Equals(LoginCAPTCHA))
            {
                MessageBox.Show("验证码输入错误", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                UpdateCAPTCHA();
                return;
            }

            string sqlStr = $"select * from Login_Admin where Name='{user_name}' and Password='{user_password}'";
            bool isAdmin = true;

            if (RadioButton_User.Checked == true)
            {
                sqlStr = $"select * from Login_User where Name='{user_name}' and Password='{user_password}'";
                isAdmin = false;
            }

            object result = SqlHelper.ExecuteScalar(sqlStr);
            if (result != null && (int)result != 0)
            {
                MessageBox.Show("登陆成功", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

                // 窗口跳转操作
                if (isAdmin)
                {
                    AdminForm adminForm = new AdminForm();
                    adminForm.Show();

                    this.Hide();
                }
                else
                {
                    UserForm userForm = new UserForm();
                    userForm.Show();

                    this.Hide();
                }
            }
            else
            {
                MessageBox.Show("登陆失败，用户名或密码错误", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                UpdateCAPTCHA();
            }
        }

        private void Button_Regist_Click(object sender, EventArgs e)
        {
            RegistForm registForm = new RegistForm();
            registForm.ShowDialog();
        }

        private void LinkLabel_ForgetPassword_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            MessageBox.Show("请与管理员联系", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
